package pl.matt.media.parser.impl;

import java.io.IOException;
import java.util.Collection;

import pl.matt.media.manager.MediaManagersFactory;
import pl.matt.media.manager.impl.ObjectsDetectorManagerImpl;
import pl.matt.media.model.ImageType;
import pl.matt.media.parser.FaceParser;
import pl.matt.media.utils.RectangleUtils;
import pl.matt.model.Rectangle;

/**
 * @author mateusz
 *
 */

public class FaceParserImpl implements FaceParser {

	public FaceParserImpl() {
		
	}
	
	@Override
	public void parse(String fileName, String destinationFolder) throws IOException {
		ObjectsDetectorManagerImpl manager = new ObjectsDetectorManagerImpl();
		String path = fileName;
		Collection<Rectangle> faces = manager.detectFaces(path);
		
		if (faces.size() == 1) {
			// pocięcie twarzy na L i R
			String dst = destinationFolder + "/face1.jpg";
			Rectangle rectangle = faces.iterator().next();
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			dst = destinationFolder + "/face1_left.jpg";
			// zapamiętanie początkowych współrzędnych
			int x = rectangle.getX();
			// przejście na współrzędne nieśrodkowe
			rectangle.setX(x);
			rectangle.setWidth(rectangle.getWidth()/2);
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			rectangle.setX(rectangle.getX() + rectangle.getWidth());
			dst = destinationFolder + "/face1_right.jpg";
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			
			// pocięcie regionów gdzie są oczy
			rectangle.setX(x);
			rectangle.setY(rectangle.getY() + (int) (rectangle.getHeight()*0.15));
			System.out.println(rectangle.getY());
			System.out.println(rectangle.getX());
			rectangle.setHeight(rectangle.getHeight()/2);
			dst = destinationFolder + "/eye1region_left.jpg";
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			String leftRegion = dst;
			dst = destinationFolder + "/eye1region_right.jpg";
			rectangle.setX(rectangle.getX() + rectangle.getWidth());
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			
			
			String rightRegion = dst;
			Collection<Rectangle> leftEyes = manager.detectLeftEyes(leftRegion);
			int i = 0;
			if (leftEyes != null) {
				for (Rectangle eye : leftEyes) {
					dst = destinationFolder + "/eye_left_" + i++ + ".jpg";
					MediaManagersFactory.getImageManager().cropImage(leftRegion, dst, eye, ImageType.JPEG);
				}
			}
			
			
			Collection<Rectangle> rightEyes = manager.detectLeftEyes(rightRegion);
			i = 0;
			if (rightEyes != null) {
				for (Rectangle eye : rightEyes) {
					dst = destinationFolder + "/eye_right_" + i++ + ".jpg";
					MediaManagersFactory.getImageManager().cropImage(rightRegion, dst, eye, ImageType.JPEG);
				}
			}

			
			// nos
			// obliczam środek lewego i prawego oka
			// w przyszłości wcześniej odrzucę błędne oczy
			
			rectangle.setX(rectangle.getX() - rectangle.getWidth());
			
			Rectangle leftEyeRegion = RectangleUtils.getAverageRectangle(leftEyes);
			Rectangle rightEyeRegion = RectangleUtils.getAverageRectangle(rightEyes);

			rectangle.setX(rectangle.getX() + leftEyeRegion.getMiddleX());
			rectangle.setY(rectangle.getY() + (leftEyeRegion.getMiddleY() + rightEyeRegion.getMiddleY())/2);
			
			rectangle.setWidth((int) (1.5*(leftEyeRegion.getMiddleX() - leftEyeRegion.getX() + rightEyeRegion.getMiddleX() - rightEyeRegion.getX())));
			rectangle.setHeight(rectangle.getWidth());
			
			dst = destinationFolder + "/nose_region.jpg";
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			String noseRegion = dst;
			
			Collection<Rectangle> noses = manager.detectNoses(noseRegion);
			i = 0;
			for (Rectangle nose : noses) {
				dst = destinationFolder + "/nose_" + i++ + ".jpg";
				MediaManagersFactory.getImageManager().cropImage(noseRegion, dst, nose, ImageType.JPEG);
			}
			
			// usta
			Rectangle nose = RectangleUtils.getAverageRectangle(noses);
			rectangle.setY(rectangle.getY() + nose.getY() + nose.getMiddleX());
			dst = destinationFolder + "/mouth_region.jpg";
			String mouthRegion = dst;
			MediaManagersFactory.getImageManager().cropImage(path, dst, rectangle, ImageType.JPEG);
			
			Collection<Rectangle> mouths = manager.detectMouths(mouthRegion);
			i = 0;
			for (Rectangle mouth : mouths) {
				dst = destinationFolder + "/mouth_" + i++ + ".jpg";
				MediaManagersFactory.getImageManager().cropImage(mouthRegion, dst, mouth, ImageType.JPEG);
			}
			
		}
		
	}

}
